#include <iostream>
#include <thread>
#include <mutex>

using std::cout;
using std::endl;
using std::thread;
using std::mutex;
using std::lock_guard;

int gCnt = 0;
mutex mtx;//创建互斥锁

void threadFunc()
{
    for(size_t idx = 0; idx != 1000000; ++idx)
    {
        lock_guard<mutex> lg(mtx);
        ++gCnt;
        //xxx 不上锁
        //yyy 上锁
        //zzz 不上锁
    }
}

int main(int argc, char *argv[])
{
    thread thA(threadFunc);
    thread thB(threadFunc);

    thA.join();
    thB.join();

    cout << "gCnt = " << gCnt << endl;

    return 0;
}

